{include file="common:header" /}
{include file="common:side" /}
<div id="page-wrapper">
  <!-- 人员管理->编辑模块 -->
  <!--*************************** content部分开始 ******************-->
  <div class="container-fluid" id="m_main">
    <div class="row m-t-20">
      <div class="col-sm-12">
        <div class="white-box">
          <h3 class="box-title m-b-20">编辑/添加 模块</h3>
          <!-- <p class="text-muted m-b-30">Data table example</p> -->
          <div class="row">
            <div class="col-sm-6">
              <div class="form-group">
                <label class="control-label col-sm-4 col-lg-3">模块名称：</label>
                <div class="col-sm-8 col-lg-9 vm">
                  <input type="text" class="form-control" placeholder="" v-model="dataInfo.mod_name" name="mod_name">
                </div>
              </div>
            </div>
          </div>
          <div class="row">
            <div class="col-sm-6">
              <div class="form-group">
                <label class="control-label col-sm-4 col-lg-3">模块标志：</label>
                <div class="col-sm-8 col-lg-9 vm">
                  <input type="text" class="form-control" placeholder="" v-model="dataInfo.nid" name="nid">
                </div>
              </div>
            </div>
          </div>
          <div class="row">
            <div class="col-sm-6">
              <div class="form-group">
                <label class="control-label col-sm-4 col-lg-3">控制器：</label>
                <div class="col-sm-8 col-lg-9 vm">
                  <input type="text" class="form-control" placeholder="" v-model="dataInfo.controller_name" name="controller_name">
                </div>
              </div>
            </div>
          </div>
          <div class="row">
            <div class="col-sm-6">
              <div class="form-group">
                <label class="control-label col-sm-4 col-lg-3">方法名称：</label>
                <div class="col-sm-8 col-lg-9 vm">
                  <input type="text" class="form-control" placeholder="" v-model="dataInfo.action_name" name="action_name">
                </div>
              </div>
            </div>
          </div>
          <div class="row">
            <div class="col-sm-6">
              <div class="form-group">
                <label class="control-label col-sm-4 col-lg-3">相对路径：</label>
                <div class="col-sm-8 col-lg-9 vm">
                  <input type="text" class="form-control" placeholder="" v-model="dataInfo.url" name="url">
                </div>
              </div>
            </div>
          </div>
          <div class="row">
            <div class="col-sm-6">
              <div class="form-group">
                <label class="control-label col-sm-4 col-lg-3">上级：</label>
                <div class="col-sm-8 col-lg-9 vm">
                  <select class="selectpicker" data-style="form-control" name="parent_id" v-model="dataInfo.parent_id" id="parent_id">
                    <option value="0">请选择</option>
                    <option v-if="upDptList.length>0" v-for="item in upDptList" :value="item.id">{{item.mod_name}}</option>
                  </select>
                </div>
              </div>
            </div>
          </div>
          <div class="row">
            <div class="col-sm-6">
              <div class="form-group">
                <label class="control-label col-sm-4 col-lg-3">状态：</label>
                <div class="col-sm-8 col-lg-9 vm">
                  <div class="radio radio-info di m-r-10">
                    <input type="radio" name="is_show" value="1" v-model="dataInfo.is_show" id="item_1">
                    <label for="item_1">显示</label>
                  </div>
                  <div class="radio radio-info di">
                    <input type="radio" name="is_show"  value="2" v-model="dataInfo.is_show" id="item_2">
                    <label for="item_2">隐藏</label>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="row">
            <div class="col-sm-6">
              <div class="form-group">
                <label class="control-label col-sm-4 col-lg-3">排序：</label>
                <div class="col-sm-8 col-lg-9 vm">
                  <input type="text" class="form-control" placeholder="" v-model="dataInfo.menu_sort" name="menu_sort">
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="ta-c m-t-40">
          <button type="button"class="btn btn-info" @click="sendMod">确认</button>
        </div>
      </div>
    </div>
    <!-- /.row -->
  </div>
  <!--*************************** content部分结束 ******************-->

  <!-- <footer class="footer text-center"> 2017 &copy; Ample Admin brought to you by themedesigner.in </footer> -->
</div>
{include file="common:js" /}
<script type="text/javascript" src="/web/js/search_list.js"></script>
{include file="common:footer" /}
{include file="component:pagination" /}
<script type="text/javascript">
  var vm1 = new Vue({
    el: '#m_main',
    data() {
      return {
        id: undefined, //当前模块id
        dataInfo: {id:'',mod_name:'',nid:'',controller_name:'',action_name:'',url:'',parent_id:'',parent_name:'',is_show:'',menu_sort:''},  //模块信息
        upDptList:[],//上级下拉列表
      }
    },
    mounted() {
      this.initSelectPicker();
      this.getUpDpt();
      this.getDataInfo()  ;
    },
    methods: {
      // 点击下拉时按最新数据渲染下拉选项
      initSelectPicker(){
        $("#parent_id").on('shown.bs.select',function(e){
          $('#parent_id').selectpicker('refresh');
        })
      },
      // 获取基础信息
      getDataInfo(){
        var that = this;
        if(window.location.href.lastIndexOf('/id/') >= 0){
          // 为编辑页面
          that.id = window.location.href.substr(window.location.href.lastIndexOf('/') + 1);
          // 获取当前编辑的模块信息
          axios.post('/dboard/adminmodules/infoData',{id:that.id},{headers: {'X-Requested-with':'XMLHttpRequest'}}).then(res=>{
            if(res.data.Code==0){
              that.dataInfo = res.data.Data;
              $('#parent_id').selectpicker('val',that.dataInfo.parent_id);
            }else{
              alert(res.data.Msg);
            }
          }).catch(err=>{})
        }
      },
      // 获取上级部门列表
      getUpDpt(){
        var that = this;
        // setTimeout(()=>{
        //   that.upDptList = [{id:1,name:'部门1'},{id:2,name:'部门2'}]
        // },2000)
        // return;
        axios.post('/dboard/adminmodules/getPmod',{},{headers: {'X-Requested-with':'XMLHttpRequest'}}).then(res=>{
          if(res.data.Code==0){
            that.upDptList = res.data.Data;
          }else{
            alert(res.data.Msg);
          }
        }).catch(err=>{})
      },
      // 提交模块信息
      sendMod(){
        var that = this;
        var dataInfo = that.dataInfo;
        that.upDptList.map(item=>{
          if(item.id == dataInfo.parent_id){
            dataInfo.parent_name = item.name;
          }
        })
        // console.log(dataInfo);
        // return;
        axios.post('/dboard/adminmodules/doAeMod',{dataInfo:dataInfo},{headers: {'X-Requested-with':'XMLHttpRequest'}}).then(res=>{
          if(res.data.Code==0){
            alert('保存成功');
            if(that.id){
              // 编辑则刷新
              window.reload();
            }else{
              console.log(res.data.Data);
              // 新增则跳转回列表
              window.location.href = "/dboard/adminmodules/lists";
              //或res返回新增部门角色id，跳转至该id的编辑页面
              // window.location.href = "/dboard/admin/permissionLists" + res.data.id;
            }
          }else{
            alert(res.data.Msg);
          }
        }).catch(err=>{})
      }
    }
  })
</script>
